package com.ita.tba.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.tba.po.Cart;
import com.ita.tba.po.Dish;
import com.ita.tba.po.DishCache;
import com.ita.tba.po.OrderList;
import com.ita.tba.service.Controller;
import com.ita.tba.service.impl.ControllerImpl;

/**
 * Servlet implementation class CheckoutServlet
 */
public class CheckoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Controller controller = new ControllerImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String email = (String) session.getAttribute("email");
		
		int uid = controller.getUid(email);
		Cart cart = controller.getThisCart(uid);
		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		Map<String, Integer> dnameNum = new HashMap<String, Integer>();
		Map<Integer, Integer> didNum = cart.getDidNum();
		dishCache = DishCache.dishCache;
		for (Integer did : didNum.keySet()) {
			dnameNum.put(dishCache.get(did).getDname(), didNum.get(did)); 
		}
		
		double totalPrice = cart.getTotalPrice();
		//dnameNum.put("TotalPrice RMB:", (int) totalPrice);
		
		request.setAttribute("email", email);
		request.setAttribute("total", totalPrice);
		request.setAttribute("dnameNum", dnameNum);
		
		request.getRequestDispatcher("/jsp/pay.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
